package com.jinlejia.erp.ui.home;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.huawei.hms.hmsscankit.ScanUtil;
import com.huawei.hms.ml.scan.HmsScan;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.erp.databinding.ActivityScanCodeLoginBinding;
import com.jinlejia.erp.ui.common.ProgressDialog;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.Map;

import okhttp3.Call;

public class ScanCodeLoginActivity extends AppCompatActivity {
    private ActivityScanCodeLoginBinding binding;
    private String qrcode;//二维码扫描结果
    private int code;//扫描状态码
    private ProgressDialog progressDialog;
    private String message;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_scan_code_login);
        binding.setEvent(this);
        progressDialog = new ProgressDialog(this,"请稍后...");
        Intent intent = getIntent();
        qrcode = intent.getStringExtra("qrcode");
        code = intent.getIntExtra("code", 0);
        message = intent.getStringExtra("message");
        if (code == 200) {
            binding.tvTaps.setText("进了家ERP电脑端登录确认");
            binding.tvTips.setText("为确保账号安全，请确认是您本人操作");
            binding.tvTips.setTextColor(Color.parseColor("#999999"));
            binding.btConfirmLogin.setText("确认登录");
            binding.btCancelLogin.setText("取消登录");
            binding.btCancelLogin.setVisibility(View.VISIBLE);
        } else if (code == 221) {
            binding.tvTaps.setText("进了家ERP电脑端登录确认");
            binding.tvTips.setText(message);
            binding.tvTips.setTextColor(Color.parseColor("#F53137"));
            binding.btConfirmLogin.setText("重新扫描");
            binding.btCancelLogin.setVisibility(View.GONE);
        } else if (code == 222) {
            binding.tvTaps.setText("进了家ERP电脑端登录确认");
            binding.tvTips.setText(message);
            binding.tvTips.setTextColor(Color.parseColor("#F53137"));
            binding.btConfirmLogin.setText("重新扫描");
            binding.btCancelLogin.setVisibility(View.GONE);
        } else if (code == 223) {
            binding.tvTaps.setText(message);
            binding.tvTips.setText("如有疑问请咨询在线客服");
            binding.tvTips.setTextColor(Color.parseColor("#999999"));
            binding.btConfirmLogin.setText("知道了");
            binding.btCancelLogin.setVisibility(View.GONE);
        } else if (code == 224) {
            binding.tvTaps.setText(message);
            binding.tvTips.setText("如有疑问请咨询在线客服");
            binding.tvTips.setTextColor(Color.parseColor("#999999"));
            binding.btConfirmLogin.setText("知道了");
            binding.btCancelLogin.setVisibility(View.GONE);
        } else if (code == 225) {
            binding.tvTaps.setText(message);
            binding.tvTips.setText("如有疑问请咨询在线客服");
            binding.tvTips.setTextColor(Color.parseColor("#999999"));
            binding.btConfirmLogin.setText("重新扫描");
            binding.btCancelLogin.setVisibility(View.GONE);
        }
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.btConfirmLogin:
                if (binding.btConfirmLogin.getText().toString().equals("知道了")) {
                    finish();
                } else if (binding.btConfirmLogin.getText().toString().equals("重新扫描")) {
                    //吊起扫码服务
                    ScanUtil.startScan(this, 111, null);
                } else if (binding.btConfirmLogin.getText().toString().equals("确认登录")) {
                    ConfirmLogin(qrcode);
                }
                break;
            case R.id.btCancelLogin:
                finish();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 111) {
            //扫码回调
            if (resultCode == RESULT_OK && data != null) {
                HmsScan obj = data.getParcelableExtra(ScanUtil.RESULT);
                if (obj != null) {
                    qrcode = obj.originalValue;
                    scanCodeLogin(obj.originalValue);
                }
            }
        }
    }

    //请求扫码登录接口
    public void scanCodeLogin(String qrcode) {
        OkHttpUtils.get()
                .url(ApiConfig.SCANCODELOGIN)
                .addParams("qrcode", qrcode)
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        int code = Integer.parseInt(String.valueOf(response.get("code")));
                        String message = String.valueOf(response.get("message"));
                        if (code == 200) {
                            binding.tvTaps.setText("进了家ERP电脑端登录确认");
                            binding.tvTips.setText("为确保账号安全，请确认是您本人操作");
                            binding.tvTips.setTextColor(Color.parseColor("#999999"));
                            binding.btConfirmLogin.setText("确认登录");
                            binding.btCancelLogin.setText("取消登录");
                            binding.btCancelLogin.setVisibility(View.VISIBLE);
                        } else if (code == 221) {
                            binding.tvTaps.setText("进了家ERP电脑端登录确认");
                            binding.tvTips.setText(message);
                            binding.tvTips.setTextColor(Color.parseColor("#F53137"));
                            binding.btConfirmLogin.setText("重新扫描");
                            binding.btCancelLogin.setVisibility(View.GONE);
                        } else if (code == 222) {
                            binding.tvTaps.setText("进了家ERP电脑端登录确认");
                            binding.tvTips.setText(message);
                            binding.tvTips.setTextColor(Color.parseColor("#F53137"));
                            binding.btConfirmLogin.setText("重新扫描");
                            binding.btCancelLogin.setVisibility(View.GONE);
                        } else if (code == 223) {
                            binding.tvTaps.setText(message);
                            binding.tvTips.setText("如有疑问请咨询在线客服");
                            binding.tvTips.setTextColor(Color.parseColor("#999999"));
                            binding.btConfirmLogin.setText("知道了");
                            binding.btCancelLogin.setVisibility(View.GONE);
                        } else if (code == 224) {
                            binding.tvTaps.setText(message);
                            binding.tvTips.setText("如有疑问请咨询在线客服");
                            binding.tvTips.setTextColor(Color.parseColor("#999999"));
                            binding.btConfirmLogin.setText("知道了");
                            binding.btCancelLogin.setVisibility(View.GONE);
                        } else if (code == 225) {
                            binding.tvTaps.setText(message);
                            binding.tvTips.setText("如有疑问请咨询在线客服");
                            binding.tvTips.setTextColor(Color.parseColor("#999999"));
                            binding.btConfirmLogin.setText("重新扫描");
                            binding.btCancelLogin.setVisibility(View.GONE);
                        }
                    }
                });
    }

    //确认登录接口
    public void ConfirmLogin(String qrcode) {
        if (progressDialog!=null){
            progressDialog.show();
        }
        OkHttpUtils.get()
                .url(ApiConfig.CONFIRMLOGIN)
                .addParams("qrcode", qrcode)
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        if (progressDialog!=null && progressDialog.isShowing()){
                            progressDialog.dismiss();
                        }
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (progressDialog!=null && progressDialog.isShowing()){
                            progressDialog.dismiss();
                        }
                        int code = Integer.parseInt(String.valueOf(response.get("code")));
                        String message = String.valueOf(response.get("message"));
                        if (code == 200) {
                            setResult(102);
                            finish();
                        } else if (code == 221) {
                            binding.tvTaps.setText("进了家ERP电脑端登录确认");
                            binding.tvTips.setText(message);
                            binding.tvTips.setTextColor(Color.parseColor("#F53137"));
                            binding.btConfirmLogin.setText("重新扫描");
                            binding.btCancelLogin.setVisibility(View.GONE);
                        } else if (code == 222) {
                            binding.tvTaps.setText("进了家ERP电脑端登录确认");
                            binding.tvTips.setText(message);
                            binding.tvTips.setTextColor(Color.parseColor("#F53137"));
                            binding.btConfirmLogin.setText("重新扫描");
                            binding.btCancelLogin.setVisibility(View.GONE);
                        } else if (code == 223) {
                            binding.tvTaps.setText(message);
                            binding.tvTips.setText("如有疑问请咨询在线客服");
                            binding.tvTips.setTextColor(Color.parseColor("#999999"));
                            binding.btConfirmLogin.setText("知道了");
                            binding.btCancelLogin.setVisibility(View.GONE);
                        } else if (code == 224) {
                            binding.tvTaps.setText(message);
                            binding.tvTips.setText("如有疑问请咨询在线客服");
                            binding.tvTips.setTextColor(Color.parseColor("#999999"));
                            binding.btConfirmLogin.setText("知道了");
                            binding.btCancelLogin.setVisibility(View.GONE);
                        } else if (code == 225) {
                            binding.tvTaps.setText(message);
                            binding.tvTips.setText("如有疑问请咨询在线客服");
                            binding.tvTips.setTextColor(Color.parseColor("#999999"));
                            binding.btConfirmLogin.setText("知道了");
                            binding.btCancelLogin.setVisibility(View.GONE);
                        }
                    }
                });
    }
}